<template>
  <aside class="sidebar">
    <!-- 其余内容不变 -->
    <div>
      <div class="header">
        <button @click="handleNewSession" class="action-btn new-session-btn" title="新建会话">
          <img src="../assets/newSess.svg" alt="" style="width: 22px; "/>
        </button>
        <button @click="handleFoldSidebar" class="action-btn fold-btn" title="折叠">
          <img src="../assets/fold.svg" alt="" style="width: 22px; "/>
        </button>
      </div>

      <div class="software-list">
        <div v-for="(item, index) in software" :key="index" class="software-item">
          <img v-if="index == 0" src="../assets/catia-v5.png" alt="" class="icon" />
          <img v-else src="../assets/catia-v6.png" alt="" class="icon" />
          <span>{{ item.name }}</span>
        </div>
      </div>

      <div class="search-bar">
        <input type="text" placeholder="搜索" class="search-input" />
      </div>

      <div class="chat-history">
        <span class="span1">聊天</span>
        <div v-for="(msg, index) in chatMessages" :key="index" class="chat-item">
          <span class="span2">{{ msg }}</span>
        </div>
      </div>
    </div>

    <div class="user-info">
      <div class="user-avatar-container" @click="toggleUserMenu">
        <img src="../assets/user-avatar.png" alt="User" class="user-avatar" />
        <span>张三</span>

        <!-- 用户菜单 -->
        <div v-if="showUserMenu" class="user-menu">
          <div class="menu-item" @click="handleProfile">
            <span>个人资料</span>
          </div>
          <div class="menu-item" @click="handleSettings">
            <span>设置</span>
          </div>
          <div class="menu-divider"></div>
          <div class="menu-item logout" @click="handleLogout">
            <span>退出登录</span>
          </div>
        </div>
      </div>
    </div>
  </aside>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const software = ref([
  { icon: '../assets/catia-v5.png', name: 'CATIA V5' },
  { icon: '../assets/catia-v6.png', name: 'CATIA V6' },
])

const chatMessages = ref([
  '前保险杠设计有哪些注意事项',
  'CATIA V5中的渲染如何使用',
  'C011车型侧围总成有多少个焊点',
  '帮我查找C011的前地板数据'
])

const showUserMenu = ref(false)

const props = defineProps({
  newSession: Function,
  foldSidebar: Function
})

const handleNewSession = () => {
  props.newSession();
}

const handleFoldSidebar = () => {
  props.foldSidebar();
}

// 切换用户菜单显示
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

// 点击其他地方关闭菜单
const closeUserMenu = (event) => {
  const userAvatarContainer = document.querySelector('.user-avatar-container')
  if (userAvatarContainer && !userAvatarContainer.contains(event.target)) {
    showUserMenu.value = false
  }
}

// 个人资料
const handleProfile = () => {
  console.log('打开个人资料')
  showUserMenu.value = false
}

// 设置
const handleSettings = () => {
  console.log('打开设置')
  showUserMenu.value = false
}

// 退出登录
const handleLogout = () => {
  // 清除登录状态
  localStorage.removeItem('isLoggedIn')
  localStorage.removeItem('username')

  // 跳转到登录页
  router.push('/')
  showUserMenu.value = false

  console.log('用户退出登录')
}

// 添加全局点击事件监听
onMounted(() => {
  document.addEventListener('click', closeUserMenu)
})

// 清理事件监听
onUnmounted(() => {
  document.removeEventListener('click', closeUserMenu)
})
</script>

<style scoped lang="scss">
.sidebar {
  width: 240px;
  background-color: #f3f3f3;
  padding: 16px;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
  height: 100%;
  overflow-y: auto;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  position: relative;
}

.header {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-start;
  gap: 8px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}

.header h2 {
  margin: 0;
  font-size: 1.2rem;
  color: #333;
}

.search-bar {
  margin-bottom: 20px;
}

.search-input {
  width: 100%;
  padding: 8px 12px;
  border-radius: 9px;
  border: 1px solid #ccc;
  outline: none;
  background-color: #e9e9e9;
}

.software-list {
  margin-bottom: 20px;
  span {
    color: #333;
    font-size: 0.8rem;
    font-weight: bolder;
  }
}

.software-item {
  display: flex;
  align-items: center;
  padding: 10px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.software-item:hover {
  background-color: #d9d9d9;
  border-radius: 12px;
}

.icon {
  width: 32px;
  height: 32px;
  margin-right: 8px;
}

.chat-history {
  margin-bottom: 20px;
  text-align: left;
  .span1 {
    font-family: '幼圆';
    font-weight: bold;
    color: #5e5b60;
  }

  .span2 {
    font-size: 0.8rem;
    font-weight: bolder;
    color: #7f7f7f;
  }
}

.chat-history h3 {
  margin: 0 0 8px 0;
  font-size: 0.9rem;
  color: #666;
}

.chat-item {
  padding: 6px 0;
  font-size: 0.9rem;
  color: #333;
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar-container {
  display: flex;
  align-items: center;
  cursor: pointer;
  position: relative;

  &:hover {
    opacity: 0.8;
  }
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}

.user-info span {
  font-size: 0.9rem;
  color: #333;
}

// 用户菜单样式
.user-menu {
  position: absolute;
  bottom: 100%;
  left: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  min-width: 160px;
  padding: 8px 0;
  z-index: 1000;
  margin-bottom: 8px;

  &::before {
    content: '';
    position: absolute;
    top: 100%;
    left: 16px;
    border: 6px solid transparent;
    border-top-color: white;
  }
}

.menu-item {
  padding: 10px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 0.9rem;
  color: #333;

  &:hover {
    background-color: #c9c8c8;
  }

  &.logout {
    color: #ef4444;

    &:hover {
      background-color: #c9c8c8;
    }
  }
}

.menu-divider {
  height: 1px;
  background-color: #e5e5e5;
  margin: 4px 0;
}

// 优化按钮样式
.action-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background-color: #ffffff;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);

  &:hover {
    transform: scale(1.05);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.12);
  }

  &:active {
    transform: scale(0.98);
  }
}

.new-session-btn {
  color: #ef4444;

  &:hover {
    background-color: #fef2f2;
  }
}

.fold-btn {
  color: #f59e0b;

  &:hover {
    background-color: #fffbeb;
  }
}
</style>
